<template>
  <div class="offline_resource_package">
      <search-table :url="url"
        ref="searchtable"
        :hideSelect="true"
        :columns="columns"
        :form="form">
          <template slot="searchForm">
            <a-form layout="inline" :form="form">
                <a-form-item label="资源包名称">
                    <a-input v-decorator="['packetName']" placeholder="请输入资源包名称">
                    </a-input>
                </a-form-item>
                <a-form-item label="资源包版本">
                    <a-input v-decorator="['packetVersion']" placeholder="请输入资源包版本">
                    </a-input>
                </a-form-item>
                <a-form-item label="生成时间">
                    <fast-range-picker
                        v-decorator="[
                        'startTime-endTime',
                        ]"
                    >
                    </fast-range-picker>
                </a-form-item>
            </a-form>
          </template>

          <template slot="oprationRow">
            <a-button type="primary" @click="createOfflinePackage">生成离线包</a-button>
          </template>

          <template slot="tableOperation" slot-scope="{record}">
            <a href="javascript:;" @click="reCreateOfflinePackage(record)" v-if="record.generateStatus==2">重新生成</a>

            <permission authority="">
              <span class="text-primary pointer " @click="packageSwitch(record)" v-if="record.isEnable ===1">已启用</span>
              <span class="text-primary pointer" @click="packageSwitch(record)" v-else>已停用</span>
            </permission>
            <a-divider type="vertical" v-show="record.isEnable !==1"></a-divider>
            <permission authority="">
              <span v-show="record.isEnable !==1" class="red text-primary pointer" @click="deletes(record)">删除</span>
            </permission>
          </template>

      </search-table>
  </div>
</template>

<script>
export default {
    data(){
        return { 
            url: "goldenhome/gh/resource/package/admin_/resource/packet/page",
            form: this.$form.createForm(this, { name: "table_search" }),
            columns:[
                {
                    title: "资源包名称",
                    dataIndex: "packetName",
                    key: "packetName",
                },
                {
                    title: "资源包版本",
                    dataIndex: "packetVersion",
                    key: "packetVersion",
                },
                {
                    title: "生成平台",
                    dataIndex: "generatePlatform",
                    key: "generatePlatform",
                    customRender: (text, row) => {
                        if(text==1){
                            return '总店端';
                        }
                        else if(text==2){
                            return '运营平台';
                        }
                        return '';
                    }
                },
                {
                    title: "生成人",
                    dataIndex: "createByName",
                    key: "createByName",
                },
                {
                    title: "生成时间",
                    dataIndex: "createDate",
                    key: "createDate",
                },
                {
                    title: "设配类型",
                    dataIndex: "deviceType",
                    key: "deviceType",
                    customRender: (text, row) => {
                        if(text==1){
                            return '大屏';
                        }
                        else if(text==2){
                            return '手机端';
                        }
                        return '';
                    }
                },
                {
                    title: "状态",
                    dataIndex: "generateStatus",
                    key: "generateStatus",
                    customRender: (text, row) => {
                        if(text==1){
                            return '成功';
                        }
                        else if(text==2){
                            return '失败';
                        }
                        else if(text==3){
                            return '生成中';
                        }
                        return '';
                    }
                },
                {
                    title: "操作",
                    dataIndex: 'operation',
                    scopedSlots: { customRender: 'operation' },
                },
            ]
        }
    },
    methods:{
        createOfflinePackage(){
            this.$router.push({
                name:'GalleryGallerySettingOfflineResourcePackageCreate'
            })
        },

        reCreatePackage(id){
            this.$post('goldenhome/gh/resource/package/admin_/resource/packet/regenerate',{
                id
            }).then(res=>{
                if(res && res.returnCode=='1000'){
                    this.$message.success('重新生成离线资源包中……')
                    this.$refs.searchtable.refreshTable();
                }
                else {
                    this.$message.error('重新生成离线资源包失败');
                }
            });
        },

        reCreateOfflinePackage(record){
            console.log(record)
            this.reCreatePackage(record.id);
        },


      packageSwitch(record) {
        console.log("record", record)
        let isEnable = record.isEnable===1?0:1
        let url = 'goldenhome/gh/resource/package/admin_/resource/packet/enable'
        if (isEnable==0){

          this.$post(url,{id: record.id,isEnable}).then(res => {
            if (res && res.returnCode == 1000) {
              if (res && res.returnCode === 1000) {
                this.$message.success('离线包已停用')
                this.$refs.searchtable.refreshTable()
              }
            }
          })
        }
        if (isEnable==1){
          this.$post(url,{id: record.id,isEnable}).then(res => {
            if (res && res.returnCode == 1000) {
              if (res && res.returnCode === 1000) {
                this.$message.success('离线包已启用')
                this.$refs.searchtable.refreshTable()
              }
            }
          })
        }

      /*  this.$confirm({
          title: '显示提示',
          content: `确认${content}吗?`,
          okText: '确认',
          cancelText: '取消',
          onOk: ()=> {

          }
        })*/
      },
      deletes(record){
        let url = 'goldenhome/gh/resource/package/admin_/resource/packet/delete'
        this.$confirm({
          title: '删除提示',
          content: '删除操作不可恢复，请确认是否删除？',
          okText: '确认删除',
          cancelText: '取消',
          onOk: () => {
            this.$post(url,{idList:[record.id]})
              .then(res => {
                if (res && res.returnCode === 1000) {
                  this.$message.success('操作成功!')
                  this.$refs.searchtable.refreshTable()
                }
              })
          }
        })

      },
    }
}
</script>

<style lang="less" scoped>
.red{
  color: red;
}
</style>